package com.dayuanit.dy15.atm.handler;

import com.dayuanit.dy15.atm.exception.BizException;
import freemarker.template.Configuration;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import javax.mail.internet.MimeMessage;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 处理邮件相关的操作
 */
@Component
public class EmailHandler {
    public EmailHandler() {
        System.out.println("EmailHandler()");
    }

    @Autowired
    private JavaMailSenderImpl javaMailSender;

    @Autowired
    private Configuration freemarkerConfiguration;

    @Value("${salt.value}")
    private String saltValue;

    @Value("${atm.host}")
    private String amtHost;

    @Value("${email.active.expire.time}")
    private int expireSecondActive;

    public void senndActiveEmail(String email) {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, "GBK");

        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.SECOND, expireSecondActive);
        Date date = cal.getTime();

        long expireTime = date.getTime();

        System.out.println(">>>>>>saltValue=" + saltValue);
        //混淆明文 明文的组成=email + 随机的字符串(8位的) + 过期时间(毫秒数)
        String msg = email + saltValue + expireTime;
        String sign = DigestUtils.md5Hex(msg);

        try {
            mimeMessageHelper.setFrom("dayuanitatm@163.com");
            mimeMessageHelper.setTo(email);
            mimeMessageHelper.setSubject("atm系统激活邮箱");

            String activeUrl = amtHost + "/email/activeEmail.do?email=" + email + "&expireTime=" + expireTime + "&sign=" + sign;

            Map<String, Object> map = new HashMap<>();
            map.put("activeUrl", activeUrl);
            map.put("host", amtHost);

            //将freemarker邮件模板转为String字符串
            String text = FreeMarkerTemplateUtils.processTemplateIntoString(freemarkerConfiguration.getTemplate("bindEmail.ftl"), map);
            System.out.println(">>>>邮件模板text=" + text);
            mimeMessageHelper.setText(text, true);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("邮件发送失败");
        }

        javaMailSender.setDefaultEncoding("GBK");
        javaMailSender.send(mimeMessage);
    }

    /**
     * 发送忘记密码的邮件
     * @param email
     */
    public void sendForgetPwdEmail(String email) {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, "GBK");

        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.SECOND, expireSecondActive);
        Date date = cal.getTime();

        long expireTime = date.getTime();

        System.out.println(">>>>>>saltValue=" + saltValue);
        //混淆明文 明文的组成=email + 随机的字符串(8位的) + 过期时间(毫秒数)
        String msg = email + saltValue + expireTime;
        String sign = DigestUtils.md5Hex(msg);

        try {
            mimeMessageHelper.setFrom("dayuanitatm@163.com");
            mimeMessageHelper.setTo(email);
            mimeMessageHelper.setSubject("ATM系统找回密码");

            String activeUrl = amtHost + "/userDetail/toRetrievePassword.do?email=" + email + "&expireTime=" + expireTime + "&sign=" + sign;

            Map<String, Object> map = new HashMap<>();
            map.put("activeUrl", activeUrl);
            map.put("host", amtHost);

            //将freemarker邮件模板转为String字符串
            String text = FreeMarkerTemplateUtils.processTemplateIntoString(freemarkerConfiguration.getTemplate("forgetPwdEmail.ftl"), map);
            System.out.println(">>>>邮件模板text=" + text);
            mimeMessageHelper.setText(text, true);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("邮件发送失败");
        }

        javaMailSender.setDefaultEncoding("GBK");
        javaMailSender.send(mimeMessage);

    }
}
